package com.ablog.service.impl;

import com.ablog.model.Admin;
import com.ablog.dao.AdminMapper;
import com.ablog.service.AdminService;
import com.ablog.exception.ServiceException;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Admin)表服务实现类
 *
 * @author mopeiwen
 * @since 2020-07-22 13:11:19
 */
@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Admin queryById(String id) {
        return this.adminMapper.selectByPrimaryKey(id);
    }

	/**
	 * 管理员登录
	 * 
	 * @param username 账号
	 * @param password 密码
	 * @return 返回管理员信息
	 */
	@Override
	public Admin login(String username, String password) {
		Admin admin = new Admin(username, password);//填入参数
		
		Admin resAdmin = this.adminMapper.selectByObj(admin);
		if (resAdmin==null) {//如果登录失败
			throw new ServiceException(-1, "管理员账号或密码错误");
		}
		
		return resAdmin;
	}
    
    /**
     * 通过Admin对象查询集合
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public List<Admin> queryAll(Admin admin) {
        return this.adminMapper.selectAll(admin);
    }

    /**
     * 新增数据
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public Admin add(Admin admin) {
        this.adminMapper.insertSelective(admin);
        return admin;
    }

    /**
     * 修改数据
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public Admin update(Admin admin) {
        this.adminMapper.updateByPrimaryKeySelective(admin);
        return this.queryById(admin.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean delById(String id) {
        return this.adminMapper.deleteByPrimaryKey(id) > 0;
    }
}